package light.head.controller.oa;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import light.head.constant.Msg;
import light.head.constant.Parm;
import light.head.constant.Url;
import light.head.dao.oa.GoodsTypeDAO;
import light.head.dao.sys.OrganDAO;
import light.head.enm.RESULT;
import light.head.framework.base.SupporterController;
import light.head.model.GoodsType;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
/***
 * 物品类型
 * @author yy
 *
 */
@Controller
@RequestMapping(Url.C_GOODSTYPE)
public class GoodsTypeController extends SupporterController{
	
	private GoodsTypeDAO gtDAO;
	private OrganDAO oDAO;

	@RequestMapping(Url.INIT)
	public ModelAndView tree(HttpServletRequest request, HttpServletResponse response) throws Exception {
		Integer oc = oDAO.getRoot(cu_organCode(request)).getCode();
		List<GoodsType> gtelist = gtDAO.getAll(oc);
		if (null != gtelist && gtelist.size() > 0) {
			StringBuffer tree = new StringBuffer();
			for (GoodsType gte : gtelist) {
				if (null != gte.getDeleted()) {
					tree.append("p.add('" + gte.getCode() + "','-1','" + gte.getName() + "');");
					aidTree(tree, gte.getSonList());
				}
			}
			request.setAttribute(Parm.TREE, tree);
		}
		return new ModelAndView(Url.GOODSTYPE_TREE);
	}
	
	private void aidTree(StringBuffer tree, List<GoodsType> list) {
		if (null == tree || null == list || list.size() == 0)
			return;
		for (GoodsType gt : list) {
			if (!gt.getDeleted() && gt.getCode() != gt.getPc()) {
				tree.append("p.add('" + gt.getCode() + "','" + gt.getPc() + "','" + gt.getName() + "');");
				aidTree(tree, gt.getSonList());
			}
		}
	}
	
	@RequestMapping(value = Url.EDIT)
	public ModelAndView edit(Integer pc,HttpServletRequest request, HttpServletResponse response) throws Exception {
		String code = request.getParameter(Parm.CODE);
		if (isNotNull(code)) {
			GoodsType goodsType = gtDAO.get(Integer.parseInt(code));
			request.setAttribute(Parm.GOODSTYPE, goodsType);
		} else if(null != pc) {
			GoodsType parent =gtDAO.getGoodsType(oDAO.getRoot(cu_organCode(request)).getCode(), pc);
			if (null != parent) {
				GoodsType goodsType = new GoodsType();
				goodsType.setPc(pc);
				goodsType.setPname(parent.getName());
				request.setAttribute(Parm.GOODSTYPE, goodsType);
			}
		}
		return new ModelAndView(Url.GOODSTYPE_EDIT);
	}
	
	@RequestMapping(Url.DELETE)
	public void delete(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String code = request.getParameter(Parm.CODE);
		if (!isNotNull(code)) {
			print(response, Msg.ERROR);
			return;
		}
		GoodsType gt = gtDAO.get(Integer.parseInt(code));
		if (null == gt)
			print(response, Msg.NOT_FIND);
		else {
			Integer rootCode = oDAO.getRoot(cu_organCode(request)).getCode(); 
			if (gtDAO.del(gt) == RESULT.SUCCESS) {
				gtDAO.loadAll(rootCode, oDAO.getSonCodeList(rootCode));
				print(response, Msg.SUCCESS);
			} else {
				print(response, Msg.FAILURE);
			}
		}
	}
	
	@RequestMapping(value = Url.SHIELD)
	public void shield(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String code = request.getParameter(Parm.CODE);
		if (!isNotNull(code)) {
			print(response, Msg.PARAMETER_ERROR);
			return;
		}
		GoodsType gt = gtDAO.get(Integer.parseInt(code));
		if (null == gt) {
			print(response, Msg.NOT_FIND);
			return;
		}
		gt.setDeleted(true);
		if (gtDAO.upd(gt) == RESULT.SUCCESS)
			print(response, Msg.SUCCESS);
		else
			print(response, Msg.ERROR);
	}
	
	@RequestMapping(value = Url.UN_SHIELD)
	public void unshield(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String code = request.getParameter(Parm.CODE);
		if (!isNotNull(code)) {
			print(response, Msg.PARAMETER_ERROR);
			return;
		}
		GoodsType gt = gtDAO.get(Integer.parseInt(code));
		if (null == gt) {
			print(response, Msg.NOT_FIND);
			return;
		}
		gt.setDeleted(false);
		if (gtDAO.upd(gt) == RESULT.SUCCESS)
			print(response, Msg.SUCCESS);
		else
			print(response, Msg.ERROR);
	}
	
	@RequestMapping(value = Url.SAVE)
	public void save (GoodsType gt, HttpServletRequest request, HttpServletResponse response) throws Exception{
		if (gt == null) {
			print(response, Msg.PARAMETER_ERROR);
			return ;
		}
		Integer oc = oDAO.getRoot(cu_organCode(request)).getCode();
		List<GoodsType> gte = gtDAO.findByNameOc(gt.getName(), oDAO.getSonCodeList(oc));
		if (gte != null && gte.size() > 0)  {
			print(response, Msg.EXISTS);
			return;
		}
		RESULT result = RESULT.ERROR;
		if (gt.getCode() == null) {
			gt.setCt(new Date());
			gt.setCu(cu_code(request));
			gt.setOc(cu_organCode(request));
			gt.setDeleted(false);
			
			result = gtDAO.add(gt);
		} else {
			result = gtDAO.update(gt);
		} if (RESULT.SUCCESS == result) {
			Integer rootCode = oDAO.getRoot(cu_organCode(request)).getCode(); 
			gtDAO.loadAll(rootCode, oDAO.getSonCodeList(rootCode));
			print(response, Msg.SUCCESS);
		} else
			print(response, Msg.ERROR);
	}
	
	
	@Autowired
	public void setGtDAO(GoodsTypeDAO gtDAO) {
		this.gtDAO = gtDAO;
	}
	@Autowired
	public void setoDAO(OrganDAO oDAO) {
		this.oDAO = oDAO;
	}
	
	
}
